<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;


/**
 * MPE 终端会员 Action
 * @package admin\action
 * @author yangjian
 */
class MemberAction extends CommonAction {

    protected $serviceBean = 'user.member.service';

    /**
     * 终端会员列表
     * @param HttpRequest $request
     */
    public function index( HttpRequest $request ) {

        $username = $request->getParameter('username', 'trim');
        $company = $request->getParameter('company', 'trim');
        $starttime = $request->getParameter('starttime', 'trim');
        $endtime = $request->getParameter('endtime', 'trim');
        $appId = $request->getParameter('app_id', 'trim');
        $page = $request->getParameter('page', 'trim');
        $pagesize = $request->getParameter('pagesize', 'trim');

        if ( $page > 0 ) $this->setPage($page);
        if ( $pagesize > 0 ) $this->setPagesize($pagesize);

        $conditions = array();

        if ( $appId ) $conditions['app_id'] = $appId;
        if ( $username ) $conditions['username'] = "%{$username}%";
        if ( $company ) $conditions['real_name'] = "%{$company}%";
        if ( $starttime ) $conditions['addtime']['>'] = $starttime.' 00:00:00';
        if ( $endtime ) $conditions['addtime']['<'] = $endtime.' 24:00:00';

        $this->loadMpes();
        $this->assign('tableHeader', array('租户标识','会员账号','实名认证','注册时间'));
        if( $request->getParameter('__export') == 1 ) { //导出数据

            $this->setConditions($conditions);
            $this->export($request);

        } else { //加载视图显示数据
            $service = Beans::get($this->getServiceBean());
            $res = $service->getMembers($conditions, null, $this->getPage(), $this->getPagesize());
            $this->assign('items', $res->getItems());

            $this->generatePageMenu($res->getCount());
            $this->setView('member/member_index');
            $this->assign('breadcrumb', array('终端会员', '会员列表'));
        }
    }

    //加载MPE列表
    protected function loadMpes() {
        $service = Beans::get('app.mpe.service');
        $mpes = $service->getItems(array('status' => 1));
        $this->assign('mpes', $mpes);
    }

    //导出数据
    protected function export(HttpRequest $request) {
        if( $request->getParameter('__export') == 1 ){
            $ids = $request->getParameter('ids');
            if( $ids ){
                $conditions['id']['$in']=$ids;
                $this->setConditions($conditions);
            }
            $this->setPagesize(getConfig('export_max_count'));
            $service = Beans::get($this->getServiceBean());
            $res = $service->getMembers($this->getConditions(), null, 1, $this->getPagesize());
            $excel = array();
            foreach($res->getItems() as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v["mpe_company"];
                $tmp[] = $v["username"];
                $tmp[] = $v['ischecked'] == 0 ? '未认证' : $v['real_name'];
                $tmp[] = $v["addtime"];
                $excel[] = $tmp;
            }
            $title = 'MPE终端会员列表';
            $header = $this->getTemplateVar('tableHeader');
            phpexcel($title,$header,$excel);
            exit();
        }
    }
}
